import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { plainToClass } from 'class-transformer';
import { IndexEnum } from 'src/common/enums/home.enum';
import { User } from 'src/core/system/user/entities/user.entity';
import { Repository } from 'typeorm';
import { AlbumService } from '../album/album.service';
import { ArtistService } from '../artist/artist.service';
import { BannerService } from '../banner/banner.service';
import { CategoryService } from '../category/category.service';
import { SongService } from '../song/song.service';
import { TrackService } from '../track/track.service';
import { VideoService } from '../video/video.service';
import { DHome } from './dao/home.dao';
import { CreateHomeDto, IndexForm } from './dto/create-home.dto';
import { UpdateHomeDto } from './dto/update-home.dto';
import { Index } from './entities/home.entity';

@Injectable()
export class HomeService {

  constructor(
    @InjectRepository(Index) private indexRepository: Repository<Index>,
    private dhome: DHome,
    private albumService: AlbumService,
    private bannerService: BannerService,
    private songService: SongService,
    private artistService: ArtistService,
    private videoService: VideoService,
    private trackService: TrackService,
    private categoryService: CategoryService,
  ) { }

  create(createHomeDto: CreateHomeDto) {

    return 'This action adds a new home';
  }

  async findAll(user: User) {
    const listIndex = await this.dhome.page();
    let indexs = []
    for await (const index of listIndex) {
      const indexDto = plainToClass(IndexForm, index);
      //轮播图
      if (index.type === IndexEnum.banner) {
        const data = await this.bannerService.getList({ pageNum: 1, pageSize: 8, bannerName: '' });
        indexDto.result = data.list
        indexDto.moduleNums = data.list.length
      }
      //金刚位
      else if (index.type === IndexEnum.jingangwei) {
        const data = [
          {
            module: "artist",
            name: "歌手"
          },
          {
            module: "billboard",
            name: "榜单"
          },
          {
            module: "tracklist",
            name: "分类歌单"
          }
        ]
        indexDto.result = data
        indexDto.moduleNums = data.length
      } else if (index.type === IndexEnum.album) {
        //秀动发行
        if (index.moreApi === 'v1/album/xdpublish') {
          const data = await this.albumService.xdpublish(user, { pageNum: 1, pageSize: 8, alubmName: '' });
          indexDto.result = data.list
          indexDto.moduleNums = data.list.length
          //最新专辑
        } else if (index.moreApi === 'v1/album/list') {
          const data = await this.albumService.findAll(user, { pageNum: 1, pageSize: 8, alubmName: '' });
          indexDto.result = data.list
          indexDto.moduleNums = data.list.length
        }

      }
      //最新歌曲
      else if (index.type === IndexEnum.song) {
        const data = await this.songService.findAll(user, { pageNum: 1, pageSize: 8, songName: '' });
        indexDto.result = data.list
        indexDto.moduleNums = data.list.length
      }
      //推荐歌手
      else if (index.type === IndexEnum.artist) {
        const data = await this.artistService.getRecommendSingers({ pageNum: 1, pageSize: 6, name: null, status: 0, startTime: null, endTime: null });
        indexDto.result = data
        indexDto.moduleNums = data.length
      }
      //推荐视频
      else if (index.type === IndexEnum.video) {
        const data = await this.videoService.getRecommendVideo(user, 1, 8);
        indexDto.result = data.list
        indexDto.moduleNums = data.list.length
      }
      //类别
      else if (index.type === IndexEnum.category) {
        const data = await this.categoryService.getCategoryListById(2);

        indexDto.result = data.slice(0, 11)
        indexDto.moduleNums = indexDto.result.length
      }

      else if (index.type === IndexEnum.tracklist) {
        //推荐歌单
        if (index.moduleName === "推荐歌单") {
          const data = await this.trackService.getRecommendList(user, { pageNum: 1, pageSize: 4, trackName: '' });
          indexDto.result = data.list
          indexDto.moduleNums = data.list.length
        }
        else if (index.moduleName === "流行") {
          const data = await this.trackService.page(user, { pageNum: 1, pageSize: 4, trackName: '', tagId: 12 });
          indexDto.result = data.list
          indexDto.moduleNums = data.list.length
        }
        else if (index.moduleName === "爵士") {
          const data = await this.trackService.page(user, { pageNum: 1, pageSize: 4, trackName: '', tagId: 19 });
          indexDto.result = data.list
          indexDto.moduleNums = data.list.length
        }
        else if (index.moduleName === "古风") {
          const data = await this.trackService.page(user, { pageNum: 1, pageSize: 4, trackName: '', tagId: 24 });
          indexDto.result = data.list
          indexDto.moduleNums = data.list.length
        }
      }



      indexs.push(indexDto)
    }
    return indexs;
  }

  findOne(id: number) {
    return `This action returns a #${id} home`;
  }

  update(id: number, updateHomeDto: UpdateHomeDto) {
    return `This action updates a #${id} home`;
  }

  remove(id: number) {
    return `This action removes a #${id} home`;
  }
}
